﻿using Learn.Service;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Learn.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LeetcodeController : ControllerBase
    {


        [HttpPost]
        public string GroupAnagrams(string[] strs)
        {
            var g = LeetcodeService.GroupAnagrams(strs);
            if (g == null)
            {
                return string.Empty;
            }
            return JsonConvert.SerializeObject(g);
        }

        [HttpPost]
        public int LongestConsecutive(int[] nums) => LeetcodeService.LongestConsecutive(nums);
        //{
        //    return LeetcodeService.LongestConsecutive(nums);
        //}

        [HttpPost]
        public void MoveZeroes(int[] nums) => LeetcodeService.MoveZeroes(nums);


        [HttpPost]
        public object ThreeSum(int[] nums) => LeetcodeService.ThreeSum(nums);


        [HttpPost]
        public object LengthOfLongestSubstring(string s)
        {
            List<string> ls = [];
            ls.Add("abcabcbb");
            ls.Add("bbbbb");
            ls.Add("pwwkew");
            ls.Add("");
            ls.Add(" ");
            ls.Add("dvdf");

            foreach (var item in ls)
            {
                LeetcodeService.LengthOfLongestSubstring(item);
            }
            return LeetcodeService.LengthOfLongestSubstring(s);

        }


        [HttpPost]
        public object FindAnagrams(string s)
        {

            return LeetcodeService.FindAnagrams(s, s);

        }

        [HttpPost]
        public object SingleNumber(int[] nums)
        {

            return LeetcodeService.SingleNumber(nums);

        }

        [HttpPost]
        public int SearchInsert(int[] nums, int target)
        {
            return LeetcodeService.SearchInsert(nums, target);
        }

        [HttpPost]
        public IList<IList<int>> Generate(int numRows) => LeetcodeService.Generate(numRows);


        [HttpPost]
        public IList<int> InorderTraversal(TreeNode root)
        {
            TreeNode tree = new()
            {
                val = 1,
                right = new()
                {
                    val = 2,
                    left = new() { val = 3 }
                }
            };
            var ll = LeetcodeService.InorderTraversal(tree);
            TreeNode treeNode = new()
            {
                val = 1,
                left = new() { val = 2, left = new() { val = 4 }, right = new() { val = 5, left = new() { val = 6 }, right = new() { val = 7 } } },
                right = new() { val = 3, right = new() { val = 8, left = new() { val = 9 } } }
            };
            var lll = LeetcodeService.InorderTraversal(treeNode);

            return LeetcodeService.InorderTraversal(root);
        }


        [HttpPost]
        public int MajorityElement(int[] nums)
        {
            return LeetcodeService.MajorityElement(nums);
        }

        [HttpPost]
        public void AddTwoNumbers()
        {
            ListNode l1, l2;
            l1 = new ListNode
            {
                val = 9,
                next = new() 
                { 
                    val = 9, 
                    next = new() 
                    {
                        val = 9,
                        next = new()
                        { 
                            val = 9,
                            next=new()
                            {
                                val = 9,
                                next= new()
                                {
                                    val = 9,
                                    next = new()
                                    {
                                        val = 9,
                                        next = null
                                    }
                                }
                            }
                        }
                    } 
                }
            };

            l2 = new ListNode()
            {
                val = 9,
                next = new() { val = 9, next = new() { val = 9, next = new() { val = 9, next = null } } }
            };

            LeetcodeService.AddTwoNumbers(l1, l2);

        }


    }
}
